const util = require("../../utils/util.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    word: [],
    value: "",
    text: [],
    scrollTop: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  onReady: function () {
    
    setTimeout(() => {
      this.setData({
        init: {
          show: true,
          content: "请输入能够表达你此刻心情的一句话，有什么不对的话请谅解，我的能力有限！！！"
        },
      })
    }, 2000)
  },

  query(text) {
    return new Promise(resolve => {
      wx.request({
        url: 'http://47.107.121.61/user/analysis',
        data: {
          text: text[text.length - 1]
        },
        method: "GET",
        header: {
          'content-type': 'application/json'
        },
        success: res => {
          const status = res.data.status;
          const data = res.data.res;
          resolve(data)
        }
      })
    })
  },

  // 处理返回数据
  codeData(data) {
    let text = this.data.text;
    let bool = false;
    let result = "";

    // 情感倾向分析
    let sentiment = data[0].items[0]
    let type = sentiment.sentiment // 情感类别，0：消极、1：中性、2：积极
    let negative = sentiment.negative_prob // 消极
    let positive = sentiment.positive_prob // 积极
    let confidence = sentiment.confidence // 置信度
    let prob = Math.max(negative, positive)

    if (positive === prob) {
      if(positive > 2 * negative) bool = true;
      // 判断正向情绪
      if (confidence > 0.2) {
        if (bool && type === 2) {
          result = "p2"
        } else if (bool === false && type === 2){
          result = "p1"
        }else {
          result = "p0"
        }
      }else {
        result = "p0"
      }
    }else{
      if (negative > 2 * positive) bool = true;
      // 判断负面情绪
      if (confidence > 0.2) {
        console.log(type)
        console.log(bool)
        if (bool && type === 0) {
          result = "n2"
        }else if (bool === false && type === 0) {
          result = "n1"
        }else {
          result = "n0"
        }
      }else {
        result = "n0"
      }
    }
    // 对象合并
    Object.assign(text[text.length - 1], {
      type: result
    })

    // 数据更新
    this.setData({
      text,
      scrollTop: this.data.scrollTop + 480
    })

    // 对话情绪分析
    let emotion = data[1].items
    text[text.length - 1].emotion = emotion[0].label
    for(let item in emotion){
      if (emotion[item].subitems.length !== 0 && emotion[item].prob >= 0.75){
        text[text.length - 1].callback = emotion[item].replies[0]
        this.setData({
          text,
          scrollTop: this.data.scrollTop + 480
        })
      }else{
        this.setData({
          text
        })
      }
    }
  },

  send() {
    let value = this.data.value;
    let text = this.data.text;
    if (value == "") return;
    if (value.length < 5) {
      util.alertView("提示", "最少5个字哦，要不然很容易判断错！", () => {})
    } else if (value.length >= 30) {
      util.alertView("提示", "你的句子太长啦，尽量控制在30字以内，可以一句一句的发哦！", () => {})
    } else{
      text.push({ self: value });
      
      this.setData({
        text,
        scrollTop: this.data.scrollTop + 480
      })
    }
    // 清空 data 里的 value
    this.data.value = "";

    // 避免 text 为空
    if (text.length === 0) return;
    this.query(text)
      .then(this.codeData.bind(this));
  },
  
  inputValue(e) {
    this.data.value = e.detail.value;
  },

  bindblur(e) {
    this.setData({
      inputInit: "", // 清空输入框中的内容
    });
  }
})